﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.MODEL.InfoConfig;
using MINERP.DAL;

namespace MINERP.BLL.InfoConfig
{
    public  class ProductOutShopBLL
    {
        /// <summary>
        /// 获得ProductOutShop列表
        /// </summary>
        /// <returns></returns>
        public List<ProductOutShopInfo> GetProductOutShopList(int productId)
        {
            List<ProductOutShopInfo> list = new List<ProductOutShopInfo>();
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                List<Tb_OutShop_ProductCategory> op = entities.Tb_OutShop_ProductCategory.Where(o => o.ProductId == productId).ToList();
                foreach (var item in op)
                {
                    if(!item.OutProcessShopReference.IsLoaded)
                    {
                        item.OutProcessShopReference.Load();
                    }
                    if(!item.Tb_ProductReference.IsLoaded)
                    {
                        item.Tb_ProductReference.Load();
                    }
                    ProductOutShopInfo poInfo = new ProductOutShopInfo();
                    poInfo.IsDefaultOutShop = item.IsDefaultOutShop;
                    poInfo.NeedTime = item.NeedTime;
                    poInfo.OutShopId = item.OutProcessShop.OutShopId;
                    poInfo.OutShopName = item.OutProcessShop.OutShopName;
                    poInfo.ProductId = item.ProductId;
                    poInfo.ProductName = item.Tb_Product.ProductName;
                    list.Add(poInfo);
                    
                }
            }
            return list;
        }

        public void Update(List<ProductOutShopInfo> list)
        {
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                try
                {
                    foreach (var item in list)
                    {
                        Tb_OutShop_ProductCategory op = entities.Tb_OutShop_ProductCategory.FirstOrDefault(o => o.ProductId == item.ProductId && o.OutShopId == item.OutShopId);
                        op.IsDefaultOutShop = item.IsDefaultOutShop;
                    }
                    entities.SaveChanges();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }

        public OutProcessShop GetProductDefaultShop(int productId)
        {
            OutProcessShop ops = null;
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                Tb_OutShop_ProductCategory op = entities.Tb_OutShop_ProductCategory.Where(o => o.ProductId == productId && o.IsDefaultOutShop == true).FirstOrDefault();
                if (op != null)
                {
                    if (!op.OutProcessShopReference.IsLoaded)
                    {
                        op.OutProcessShopReference.Load();
                        ops = op.OutProcessShop;
                    }
                }
            }
            return ops;
 
        }
    }
}
